import finalshu.FinalShu;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * 退出按钮
 */
@WebServlet("/loginOut")
public class LoginOut extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session =  req.getSession();
        //退出时吧登录信息时间删除
        String username= (String) session.getAttribute(FinalShu.FINAL_ID);
        ServletContext sc = this.getServletContext();
        Map loginList = (Map)sc.getAttribute(FinalShu.FINAL_ID_LIST);
        loginList.put(username,null);
        sc.setAttribute(FinalShu.FINAL_ID_LIST,loginList);

        if (null!= session){
            session.invalidate();
        }
        resp.sendRedirect(req.getContextPath()+"/index.jsp");
    }
}
